package fr.upmc.i3s.upmcontact.views.fragments;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import fr.upmc.i3s.upmcontact.R;
import fr.upmc.i3s.upmcontact.views.FragmentController;
import fr.upmc.i3s.upmcontact.views.fragments.models.IFragmentModel;
import fr.upmc.i3s.upmcontact.views.fragments.models.PageFragmentModel;

public class WebViewFragment extends PageFragmentModel implements IFragmentModel {
	
	private static final String EXTRA_URL = "extras.url";
	
	private static volatile WebViewFragment INSTANCE;
	private FragmentController fragmentController;
	
	private WebView wvContent;
	private TextView tvFeetLeft;
	private TextView tvFeetRight;
	private ImageView ivArrowLeft;
	private ImageView ivArrowRight;
	
	private String url;
	
	public static WebViewFragment newInstance(String url) {
		if(INSTANCE == null) {
			WebViewFragment fragment = new WebViewFragment();
		    Bundle bundle = new Bundle(4);
		    bundle.putInt(EXTRA_LAYOUT_FRAGMENT, R.layout.fragment_web_view);
		    bundle.putInt(EXTRA_HEAD_ICON, R.drawable.ic_menu_info_white);
		    bundle.putInt(EXTRA_HEAD_TEXT, R.string.title_fragment_infos);
		    bundle.putString(EXTRA_URL, url);
		    fragment.setArguments(bundle);
		    INSTANCE = fragment;
		}
	    return INSTANCE;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(getArguments() != null) {
        	url = getArguments().getString(EXTRA_URL);
        }
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.initFragmentViews();
		this.fragmentController = (FragmentController) getActivity();
		fetchViews();
		customizeViews();
		setListeners();
	}
 	
 	@Override
 	public void fetchViews() {
 		this.wvContent = (WebView) this.getView().findViewById(R.id.webView_content);
 		this.tvFeetLeft = (TextView) this.getView().findViewById(R.id.textView_feet_text_left);
 		this.tvFeetRight = (TextView) this.getView().findViewById(R.id.textView_feet_text_right);
 		this.ivArrowLeft = (ImageView) this.getView().findViewById(R.id.imageView_feet_arrow_left);
 		this.ivArrowRight = (ImageView) this.getView().findViewById(R.id.imageView_feet_arrow_right);
 	}
 	
 	@SuppressLint("SetJavaScriptEnabled")
	@Override
 	public void customizeViews() {
 		this.fragmentController.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
 		this.tvFeetLeft.setText("");
 		this.tvFeetRight.setText("");
 		this.ivArrowLeft.setVisibility(View.GONE);
 		this.ivArrowRight.setVisibility(View.GONE);
 		this.wvContent.getSettings().setJavaScriptEnabled(true);
 		this.wvContent.getSettings().setBuiltInZoomControls(true);
 		this.wvContent.getSettings().setDisplayZoomControls(true);
 		this.wvContent.getSettings().setLoadWithOverviewMode(true);
 		this.wvContent.getSettings().setUseWideViewPort(true);
 		this.wvContent.setWebViewClient(new WebViewClient() {
 		    public boolean shouldOverrideUrlLoading(WebView view, String url){
 		        view.loadUrl(url);
 		        return false;
 		   }
 		});
		this.wvContent.loadUrl(url);
 	}
 	
 	@Override
 	public void setListeners() {
 	}
 	
}
